package com.gonsin.gview.model.dynamic;

import com.gonsin.gview.annotations.board.table.GProp;
import com.gonsin.gview.annotations.enums.ButtonAction;
import com.gonsin.gview.annotations.enums.ButtonStyle;
import com.gonsin.gview.annotations.enums.ButtonType;
import com.gonsin.gview.annotations.impl.EmptyBatchActionImpl;
import com.gonsin.gview.annotations.impl.EmptyConfirmActionImpl;
import com.gonsin.gview.annotations.impl.EmptyFileActionImpl;
import com.gonsin.gview.annotations.interfaces.IBatchActionListener;
import com.gonsin.gview.annotations.interfaces.IConfirmActionListener;
import com.gonsin.gview.annotations.interfaces.IFileActionListener;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DynamicButton {

    /**
     * 按钮显示的名字
     */
    private String name;

    /**
     * 按钮前显示的小图标
     */
    private String icon;

    /**
     * 按钮点击所执行的内容
     */
    private ButtonAction action;

    /**
     * 直接修改浏览器的路径跳转到某个连接
     */
    private String link;

    /**
     * 跳转到某个view，action=PAGE有效
     */
    private String view;

    /**
     * action = OPEN_DIALOG、PAGE、link有效 传递的参数
     */
    private List<DynamicProp> props;

    /**
     * 点击批量操作之后的回调 action = batch_action时有效
     */
    private Class<? extends IBatchActionListener> batchAction;

    /**
     * 点击批量操作之后的回调 action = batch_action时有效
     */
    private String batchActionClass;

    /**
     * 点击上传文件之后的回调，action = File_upload时有效
     */
    private Class<? extends IFileActionListener> fileAction;

    /**
     * 点击上传文件之后的回调，action = File_upload时有效
     */
    private String fileActionClass;

    /**
     * 确认执行某种事情的回调，action = Confirm时有效
     */
    private IConfirmActionListener confirmAction;

    /**
     * 确认信息的事件回调，action = Confirm时有效
     */
    private String confirmActionClass;

    /**
     * 弹出确认框的提示语，action = Confirm时有效
     */
    private String confirmMessage;

    /**
     * 下载路径,action = Download时有效
     */
    private String downloadUrl;

    /**
     * 文本颜色
     */
    private String textColor = "#FFFFFF";


    /**
     * 按钮的颜色
     */
    private ButtonType buttonType = ButtonType.DEFAULT;


    /**
     * 按钮的风格
     */
    private ButtonStyle buttonStyle = ButtonStyle.DEFAULT;

    /**
     * 事件传递时候，调用的@GAction的名字，action = UPLOAD_FILE\DOWNLOAD\BATCH\CONFIRM 时候有效
     */
    private String actionName;

    /**
     * 支持的文件格式，例如   .apk,.jar,.gonsin,。jpg
     */
    private String accept = ".*";

    /**
     * 接收文件个数，如果多个，收到的字符串为  /files/xxx.xx,/files/xxx.yy
     */
    private int limit = 1;

    /**
     * 根据条件隐藏或显示
     */
    private String showIf = "1";

    /**
     * 根据条件禁用
     */
    private String disableIf = "0";


    @ApiModelProperty("可支持的文件上传类型，如果是file，表示只支持文件，" +
            "如果是path，表示支持文件和文件夹")
    private String fileUploadType = "file";


    @ApiModelProperty("是否为外部链接")
    private Boolean outLink;


    /**
     * 2024/0/24 新增
     * 从session中获取权限，如果权限列表不包含该值，则不显示此按钮
     */
    private String[] permission;


    public static DynamicButtonBuilder builder() {
        return new DynamicButtonBuilder();
    }

    public static final class DynamicButtonBuilder {
        /**
         * 按钮显示的名字
         */
        private String name;

        /**
         * 按钮前显示的小图标
         */
        private String icon;

        /**
         * 按钮点击所执行的内容
         */
        private ButtonAction action;

        /**
         * 直接修改浏览器的路径跳转到某个连接
         */
        private String link;

        /**
         * 跳转到某个view，action=PAGE有效
         */
        private String view;

        /**
         * action = OPEN_DIALOG、PAGE、link有效 传递的参数
         */
        private List<DynamicProp> props;

        /**
         * 点击批量操作之后的回调 action = batch_action时有效
         */
        private Class<? extends IBatchActionListener> batchAction;

        /**
         * 点击批量操作之后的回调 action = batch_action时有效
         */
        private String batchActionClass;

        /**
         * 点击上传文件之后的回调，action = File_upload时有效
         */
        private Class<? extends IFileActionListener> fileAction;

        /**
         * 点击上传文件之后的回调，action = File_upload时有效
         */
        private String fileActionClass;

        /**
         * 确认执行某种事情的回调，action = Confirm时有效
         */
        private IConfirmActionListener confirmAction;

        /**
         * 确认信息的事件回调，action = Confirm时有效
         */
        private String confirmActionClass;

        /**
         * 弹出确认框的提示语，action = Confirm时有效
         */
        private String confirmMessage;

        /**
         * 下载路径,action = Download时有效
         */
        private String downloadUrl;

        /**
         * 文本颜色
         */
        private String textColor = "#FFFFFF";


        /**
         * 按钮的颜色
         */
        private ButtonType buttonType = ButtonType.DEFAULT;


        /**
         * 按钮的风格
         */
        private ButtonStyle buttonStyle = ButtonStyle.DEFAULT;

        /**
         * 事件传递时候，调用的@GAction的名字，action = UPLOAD_FILE\DOWNLOAD\BATCH\CONFIRM 时候有效
         */
        private String actionName;

        /**
         * 支持的文件格式，例如   .apk,.jar,.gonsin,。jpg
         */
        private String accept = ".*";

        /**
         * 接收文件个数，如果多个，收到的字符串为  /files/xxx.xx,/files/xxx.yy
         */
        private int limit = 1;

        /**
         * 根据条件隐藏或显示
         */
        private String showIf = "1";

        /**
         * 根据条件禁用
         */
        private String disableIf = "0";

        @ApiModelProperty("可支持的文件上传类型，如果是file，表示只支持文件，" +
                "如果是path，表示支持文件和文件夹")
        private String fileUploadType = "file";


        @ApiModelProperty("是否为外部链接")
        private Boolean outLink;


        /**
         * 2024/0/24 新增
         * 从session中获取权限，如果权限列表不包含该值，则不显示此按钮
         */
        private String[] permission;

        private DynamicButtonBuilder() {
        }

        public DynamicButtonBuilder name(String name) {
            this.name = name;
            return this;
        }

        public DynamicButtonBuilder icon(String icon) {
            this.icon = icon;
            return this;
        }

        public DynamicButtonBuilder action(ButtonAction action) {
            this.action = action;
            return this;
        }

        public DynamicButtonBuilder link(String link) {
            this.link = link;
            return this;
        }

        public DynamicButtonBuilder view(String view) {
            this.view = view;
            return this;
        }

        public DynamicButtonBuilder props(List<DynamicProp> props) {
            this.props = props;
            return this;
        }

        public DynamicButtonBuilder batchAction(Class<? extends IBatchActionListener> batchAction) {
            this.batchAction = batchAction;
            return this;
        }

        public DynamicButtonBuilder batchActionClass(String batchActionClass) {
            this.batchActionClass = batchActionClass;
            return this;
        }

        public DynamicButtonBuilder fileAction(Class<? extends IFileActionListener> fileAction) {
            this.fileAction = fileAction;
            return this;
        }

        public DynamicButtonBuilder fileActionClass(String fileActionClass) {
            this.fileActionClass = fileActionClass;
            return this;
        }

        public DynamicButtonBuilder confirmAction(IConfirmActionListener confirmAction) {
            this.confirmAction = confirmAction;
            return this;
        }

        public DynamicButtonBuilder confirmActionClass(String confirmActionClass) {
            this.confirmActionClass = confirmActionClass;
            return this;
        }

        public DynamicButtonBuilder confirmMessage(String confirmMessage) {
            this.confirmMessage = confirmMessage;
            return this;
        }

        public DynamicButtonBuilder downloadUrl(String downloadUrl) {
            this.downloadUrl = downloadUrl;
            return this;
        }

        public DynamicButtonBuilder textColor(String textColor) {
            this.textColor = textColor;
            return this;
        }

        public DynamicButtonBuilder buttonType(ButtonType buttonType) {
            this.buttonType = buttonType;
            return this;
        }

        public DynamicButtonBuilder buttonStyle(ButtonStyle buttonStyle) {
            this.buttonStyle = buttonStyle;
            return this;
        }

        public DynamicButtonBuilder actionName(String actionName) {
            this.actionName = actionName;
            return this;
        }

        public DynamicButtonBuilder accept(String accept) {
            this.accept = accept;
            return this;
        }

        public DynamicButtonBuilder limit(int limit) {
            this.limit = limit;
            return this;
        }

        public DynamicButtonBuilder showIf(String showIf) {
            this.showIf = showIf;
            return this;
        }

        public DynamicButtonBuilder disableIf(String disableIf) {
            this.disableIf = disableIf;
            return this;
        }

        public DynamicButtonBuilder fileUploadType(String fileUploadType) {
            this.fileUploadType = fileUploadType;
            return this;
        }

        public DynamicButtonBuilder outLink(Boolean outLink) {
            this.outLink = outLink;
            return this;
        }

        public DynamicButtonBuilder permission(String[] permission) {
            this.permission = permission;
            return this;
        }

        public DynamicButton build() {
            DynamicButton dynamicButton = new DynamicButton();
            dynamicButton.setName(name);
            dynamicButton.setIcon(icon);
            dynamicButton.setAction(action);
            dynamicButton.setLink(link);
            dynamicButton.setView(view);
            dynamicButton.setProps(props);
            dynamicButton.setBatchAction(batchAction);
            dynamicButton.setBatchActionClass(batchActionClass);
            dynamicButton.setFileAction(fileAction);
            dynamicButton.setFileActionClass(fileActionClass);
            dynamicButton.setConfirmAction(confirmAction);
            dynamicButton.setConfirmActionClass(confirmActionClass);
            dynamicButton.setConfirmMessage(confirmMessage);
            dynamicButton.setDownloadUrl(downloadUrl);
            dynamicButton.setTextColor(textColor);
            dynamicButton.setButtonType(buttonType);
            dynamicButton.setButtonStyle(buttonStyle);
            dynamicButton.setActionName(actionName);
            dynamicButton.setAccept(accept);
            dynamicButton.setLimit(limit);
            dynamicButton.setShowIf(showIf);
            dynamicButton.setDisableIf(disableIf);
            dynamicButton.setFileUploadType(fileUploadType);
            dynamicButton.setOutLink(outLink);
            dynamicButton.setPermission(permission);
            return dynamicButton;
        }
    }
}
